package com.handydata.antifraud.ws.dap;

import java.util.Date;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.to.dap.AFuScoreReqTO;
import com.handydata.antifraud.to.dap.AFuScoreResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.ws.WsInvocationService;
import com.handydata.common.util.Md5Utils;

/**
 * 阿福评分
 *
 */
@Slf4j
@Service("aFuScoreWsService")
@Comment("阿福评分")
public class AFuScoreWsServiceImpl extends
		DataPlatformWsService<AFuScoreReqTO, AFuScoreResTO> implements
		WsInvocationService<AFuScoreReqTO, AFuScoreResTO> {
	
	@Override
	public AFuScoreResTO query(AFuScoreReqTO req){
		log.info("阿福评分查询请求参数 {}", req);
		AFuScoreResTO res = new AFuScoreResTO();
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("阿福评分查询参数校验 {}", "phoneNum为空");
			res.setResCode("1111");
			res.setResMsg("阿福评分查询接口phoneNum参数为空");
			return res;
		}
		if (StringUtils.isEmpty(req.getName())) {
			log.error("阿福评分查询参数校验 {}", "name为空");
			res.setResCode("1111");
			res.setResMsg("阿福评分查询接口name参数为空");
			return res;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("阿福评分查询参数校验 {}", "idCard为空");
			res.setResCode("1111");
			res.setResMsg("阿福评分查询接口idCard参数为空");
			return res;
		}
		this.dataType = DataSourceCode.A_FU_SCORE.getCode();
		try {
			res = executeQuery(req);
		} catch (Exception e) {
			res.setResCode("1111");
			res.setResMsg("执行外部数据源异常");
		}
		return res;
	}

	@Override
	protected String createQueryXml(AFuScoreReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>2.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>300671</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<cid>").append(Md5Utils.md5ToHexStr(req.getIdCard())).append("</cid>");
//		xml.append("<cid>").append(req.getIdCard()).append("</cid>");
		xml.append("<name>").append(Md5Utils.md5ToHexStr(req.getName())).append("</name>");
//		xml.append("<name>").append(req.getName()).append("</name>");
		xml.append("<phone>").append(Md5Utils.md5ToHexStr(req.getPhoneNum())).append("</phone>");
//		xml.append("<phone>").append(req.getPhoneNum()).append("</phone>");
		xml.append("<queryReason>LOAN_MANAGE</queryReason>");
		xml.append("</subatm>");
		log.info("阿福评分查询调用翰迪数据平台请求报文{}", xml);
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected AFuScoreResTO parseResultAndSave(String resultXml,
			String orderId, Date createDate) {
		log.info("调用阿福评分查询接口返回报文：{}", resultXml);
		AFuScoreResTO resTO = new AFuScoreResTO();
		resTO.setAfMidscore(9999);
		resTO.setAfMidsuggest(9999);
		try {
			// 解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if (null != jasonObject) {
				if ("0000".equals(jasonObject.get("resCode"))) {
					JSONObject data = JSONObject.parseObject(jasonObject.getString("resData"));
					if (StringUtils.isNotBlank(data.getString("compositeScore"))) {
						double afMidscore = Double.parseDouble(data.getString("compositeScore"));
						resTO.setAfMidscore(afMidscore);
					}
					if (StringUtils.isNotBlank(data.getString("decisionSuggest"))) {
						int afMidsuggest = Integer.parseInt(data.getString("decisionSuggest"));
						resTO.setAfMidsuggest(afMidsuggest);
					}
				}
				resTO.setResCode((String) jasonObject.get("resCode"));
				resTO.setResMsg((String) jasonObject.get("resMsg"));
			} else {
				resTO.setResCode("0001");
				resTO.setResMsg("阿福评分查询接口返回失败");
			}
		} catch (Exception e) {
			log.info("调用阿福评分查询接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("调用阿福评分查询接口异常");
		}
		log.info("调用阿福评分查询接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, AFuScoreReqTO.class);
	}

}
